require 'digest'
class User < ActiveRecord::Base
  belongs_to :group
  has_many :containers
  has_many :notes
  has_many :bills
  has_many :attachments
  validates :name, presence: true, length: {maximum: 50}, uniqueness: true

  validates :email, presence: true, length: {maximum: 255}, uniqueness: true
  validates :password, presence: true, length: 6..256


    before_create :digest_password
    # before_save :create_note
    before_create :create_note

    # before_update :update_digest_password

    def digest_password
        self.password = User.password_digest(self.password)
    end
    def self.password_digest(password)
        # Digest::MD5.hexdigest password
        BCrypt::Password.create(password).to_s
    end

    def update_digest_password
      Rails.logger.debug "password = #{password}"
      if password
        digest_password
      end
    end

    def create_note
        self.notes.build(payment: 0.0, total: 0.0, kind: "zero")
    end

    def default_icon_str
      Pinyin.t(self.name)
    end
end
